package com.es.piomessages;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

import com.es.piomessages.model.AlarmeEntrega;
import com.es.piomessages.model.Menssagem;

public class MainActivity extends TabActivity{
	private TabHost tabHost;
	private PendingIntent notifyIntent;
	public static Intent alarmIntent;
	private static boolean firstOpen = false;

	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);

		PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
		String time = sharedPref.getString("time", SettingsActivity.DEFAULT_TIME);
		
		alarmIntent = new Intent(this, AlarmeEntrega.class);
		notifyIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);

		if(!firstOpen){
			firstOpen = true;
			try {
				SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
				Date date = dateFormat.parse(time);

				GregorianCalendar cal = new GregorianCalendar();
				cal.set(Calendar.HOUR_OF_DAY, date.getHours());
				cal.set(Calendar.MINUTE, date.getMinutes());
				cal.set(Calendar.SECOND, 2);

				AlarmManager mgr = (AlarmManager) getSystemService(ALARM_SERVICE);
				mgr.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, notifyIntent);

			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		

		tabHost = getTabHost();

		Menssagem msgFromIntent = (Menssagem) getIntent().getSerializableExtra("message");
		TabSpec messageSpec = tabHost.newTabSpec("Messages");
		messageSpec.setIndicator(getString(R.string.message_tab), getResources().getDrawable(android.R.drawable.ic_dialog_email));
		Intent messageIntent = new Intent(this, MessageActivity.class);
		messageIntent.putExtra("message", msgFromIntent);
		
		messageSpec.setContent(messageIntent);

		TabSpec favoritaSpec = tabHost.newTabSpec("Favoritas");
		favoritaSpec.setIndicator(getString(R.string.favorite_tab), getResources().getDrawable(android.R.drawable.btn_star));
		Intent favoritasIntent = new Intent(this, FavoriteActivity.class);
		favoritaSpec.setContent(favoritasIntent);

		TabSpec configSpec = tabHost.newTabSpec("Sobre");
		configSpec.setIndicator(getString(R.string.about_tab), getResources().getDrawable(android.R.drawable.ic_dialog_info));
		Intent settingsIntent = new Intent(this, AboutActivity.class);
		configSpec.setContent(settingsIntent);

		tabHost.addTab(messageSpec);
		tabHost.addTab(favoritaSpec);
		tabHost.addTab(configSpec);

	}
	public void addfavorityActivity(){
		TabSpec favoritaSpec = (TabSpec) tabHost.getTag(1);
		tabHost.setTag(1, favoritaSpec);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.preferenes:
			Intent settingsIntent = new Intent(this, SettingsActivity.class);
			settingsIntent.putExtra("alarmIntent", alarmIntent);
			startActivity(settingsIntent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}